package com.youkeda.meetingroom.util;


import com.youkeda.meetingroom.constant.CommonConstant;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 格式检查
 *
 * @author Awake
 * @date 2018-12-25 10:51
 **/
public class FormatUtils {

    /**
     * 验证邮箱格式
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        if (email == null) {
            return false;
        }
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(email);
        if (m.matches()) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 验证密码格式
     * 包含字母 数字 还有 @&#$,.
     * 长度6-18
     *
     * @param pwd
     * @return
     */
    public static boolean checkPwd(String pwd) {
        if (pwd == null) {
            return false;
        }
        if (pwd.length() < CommonConstant.MIN_PWD_LENGTH || pwd.length() > CommonConstant.MAX_PWD_LENGTH) {
            return false;
        }
        /*合法的组成部分*/
        //String[]collectPart= {"_","@","&","#","$","."};
        String part = "@&#$,.";
        for (int i = 0; i < pwd.length(); i++) {
            /*如果不为字母和数字*/
            if (!(Character.isLetterOrDigit(pwd.charAt(i)))) {
                if (!(part.contains(Character.toString(pwd.charAt(i))))) {
                    return false;
                }
            }
        }
        return true;
    }
}
